﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Principal;

namespace Steve.UI.Site
{
    public static class SecurityExtensions
    {
        public static bool IsInRoles(this IPrincipal principal, params string[] roles)
        {
            return IsInRoles(principal, false, roles);
        }

        public static bool IsInRoles(this IPrincipal principal, bool allRoles, params string[] roles)
        {
            if (roles == null || roles.Length == 0) throw new ArgumentNullException("roles");

            foreach (var r in roles)
            {
                if (principal.IsInRole(r))
                {
                    if(!allRoles) return true;
                }
                else if (allRoles)
                {
                    return false;
                }
            }

            return allRoles;
        }
    }
}